package com.weishui.talking.app;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * 
 * @description: 抽象功能模板
 * @author: liya.xiao 
 * @version: 1.0.0
 * @modify:
 * @Copyright: watchdata
 */
public class AbstractPanle extends JDialog {

	private static final long serialVersionUID = 1L;
	private static final SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");

	protected static TextPaneMenu logTextArea;
	protected JScrollPane jsPan;

	//日志框中日志当前行数
	public static int logCount = 0;
	//日志框中日志最大行数
	private static final int logLimitCount = 1000;
	
	public static int color = 0;

	private static SimpleAttributeSet attrSetOdd; // 奇数行属性
	private static SimpleAttributeSet attrSetEven; // 偶数行属性
	private static SimpleAttributeSet attrSetError; // 错误日志属性

	public AbstractPanle(JFrame owner, boolean modal) {
		super(owner, modal);
		logTextArea = new TextPaneMenu();
		logTextArea.setEditable(false);
		logTextArea.setVisible(true);

		jsPan = new JScrollPane(logTextArea);
		jsPan.setViewportView(logTextArea);

		attrSetOdd = new SimpleAttributeSet();
		attrSetEven = new SimpleAttributeSet();
		attrSetError = new SimpleAttributeSet();
		StyleConstants.setForeground(attrSetOdd, Color.blue);
		StyleConstants.setForeground(attrSetEven, Color.black);
		StyleConstants.setForeground(attrSetError, Color.red);
		StyleConstants.setBold(attrSetError, true);
		StyleConstants.setFontSize(attrSetOdd, 12);
		StyleConstants.setFontSize(attrSetEven, 12);
	}

	/**
	 * 增加普通日志，隔行隔色
	 * 
	 * @param logStr
	 */
	public static void addLog(String logStr) {
		String info = SF.format(new Date()) + "\n     " + logStr + "\n";
		if (AbstractPanle.color == 0) {
			addLog(info, logTextArea, attrSetEven);
			AbstractPanle.color = 1;
		} else {
			addLog(info, logTextArea, attrSetOdd);
			AbstractPanle.color = 0;
		}

	}

	/**
	 * 增加错误日志，显示红色粗
	 * 
	 * @param logStr
	 */
	public static void addErrorLog(String logStr) {
		String info = SF.format(new Date())+ logStr + "\n";
		addLog(info, logTextArea, attrSetError);

	}

	private static void addLog(String text, JTextPane textPane, SimpleAttributeSet attr) {
		StyledDocument doc = textPane.getStyledDocument();
		//如果当前行数大于最大行数，先清空日志框中日志，再打印新的日志
		if(logCount > logLimitCount){
			try {
				doc.remove(0, doc.getLength());
				AbstractPanle.color = 0;
				logCount = 0;
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
		try {
			doc.insertString(doc.getLength(), text, attr);
			textPane.setCaretPosition(doc.getLength());
			logCount ++;
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 清空日志框
	 */
	public static void removeLog(){
		StyledDocument doc = logTextArea.getStyledDocument();
		try {
			doc.remove(0, doc.getLength());
			AbstractPanle.color = 0;
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}
